package cn.slipi.admin.controller;

import cn.slipi.admin.biz.AdminBiz;
import cn.slipi.admin.biz.vo.admin.AdminLogVo;
import cn.slipi.admin.biz.vo.admin.AdminVo;
import cn.slipi.admin.biz.vo.admin.TokenVo;
import cn.slipi.admin.common.annotation.NoLogin;
import cn.slipi.admin.common.annotation.Permission;
import cn.slipi.admin.common.dto.PageDTO;
import cn.slipi.admin.common.exception.base.BizException;
import cn.slipi.admin.common.response.JsonResponse;
import cn.slipi.admin.common.utils.ImgAuthCode;
import cn.slipi.admin.constants.AdminConstants;
import cn.slipi.admin.constants.permission.PermissionConstants;
import cn.slipi.admin.controller.form.admin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * 用户接口
 *
 * @author lee
 * @date 2019-03-17
 * @since 1.0.0
 */


@Validated
@Controller
@ResponseBody
@RequestMapping(AdminConstants.CAR_API_PREFIX + "/admin")
public class AdminController {

    @Autowired
    private AdminBiz adminBiz;

    @PutMapping("/update/{adminId}")
    @Permission(value = {PermissionConstants.ADMIN_UPDATE})
    public JsonResponse<Boolean> update(@PathVariable("adminId") Integer adminId, @Valid @RequestBody AdminUpdateForm adminUpdateForm) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.adminBiz.updateAdmin(adminId, adminUpdateForm);
        responseVo.setData(result);
        return responseVo;
    }

    @PutMapping("/c/password/{adminId}")
    @Permission(value = {PermissionConstants.ADMIN_CHANGE_PASSWORD})
    public JsonResponse<Boolean> changePassword(@PathVariable("adminId") Integer adminId, @Valid @RequestBody ChangePasswordForm changePasswordForm) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        responseVo.setData(this.adminBiz.changePassword(adminId, changePasswordForm));
        return responseVo;
    }

    @PostMapping("/list")
    @Permission(value = {PermissionConstants.ADMIN_GET_LIST})
    public JsonResponse<PageDTO<AdminVo>> getList(@Valid @RequestBody AdminQueryForm adminQueryForm) {
        JsonResponse<PageDTO<AdminVo>> responseVo = new JsonResponse<>();
        PageDTO<AdminVo> result = this.adminBiz.getAdminList(adminQueryForm);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 用户管理员信息
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    @Permission(value = {PermissionConstants.ADMIN_GET_DETAIL})
    public JsonResponse<AdminVo> getDetail(@Valid @RequestParam("id") Long id) {
        JsonResponse<AdminVo> responseVo = new JsonResponse<>();
        AdminVo adminVo = this.adminBiz.getDetail(id);
        responseVo.setData(adminVo);
        return responseVo;
    }

    /**
     * 初始化密码
     *
     * @param id
     * @return
     */
    @GetMapping("/init_pass/{id}")
    @Permission(value = {PermissionConstants.ADMIN_INIT_PASSWORD})
    public JsonResponse<Boolean> initPassword(@Valid @PathVariable("id") Integer id) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.adminBiz.initPassword(id);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 获取token
     *
     * @param getTokenForm
     * @return
     */
    @NoLogin
    @PostMapping("/token")
    public JsonResponse<TokenVo> getToken(@RequestBody GetTokenForm getTokenForm) {
        JsonResponse<TokenVo> responseVo = new JsonResponse<>();
        responseVo.setData(this.adminBiz.getToken(getTokenForm));
        return responseVo;
    }

    /**
     * 添加管理员用户
     *
     * @param adminCreateForm
     * @return
     */
    @PostMapping("/add")
    @Permission(value = {PermissionConstants.ADMIN_ADD})
    public JsonResponse<Integer> add(@Valid @RequestBody AdminCreateForm adminCreateForm) {
        JsonResponse<Integer> responseVo = new JsonResponse<>();
        Integer result = this.adminBiz.addAdmin(adminCreateForm);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 验证图片验证码
     *
     * @param code
     * @return
     */
    @NoLogin
    @GetMapping("/img/verify")
    public JsonResponse<Boolean> verifyImageCode(@RequestParam("code") String code) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.adminBiz.checkImageCode(code);
        responseVo.setData(result);
        return responseVo;
    }

    /**
     * 图片验证码
     *
     * @param request
     * @param response
     */
    @NoLogin
    @GetMapping("/img/code")
    public void imageCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 设置响应的类型格式为图片格式
            response.setContentType("image/jpeg");
            // 禁止图像缓存
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
            ImgAuthCode imgAuthCode = this.adminBiz.getImgAuthCode();
            try {
                imgAuthCode.write(response.getOutputStream());
            } catch (IOException e) {
                throw new BizException(10000, "img.code.write.failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户行为列表
     *
     * @param adminLogQueryForm
     * @return
     */
    @PostMapping("/log/list")
    @Permission(value = {PermissionConstants.ADMIN_LOG_GET_LIST})
    public JsonResponse<PageDTO<AdminLogVo>> getLogList(@Valid @RequestBody AdminLogQueryForm adminLogQueryForm) {
        JsonResponse<PageDTO<AdminLogVo>> responseVo = new JsonResponse<>();
        PageDTO<AdminLogVo> result = this.adminBiz.getList(adminLogQueryForm);
        responseVo.setData(result);
        return responseVo;
    }

}
